package com.attendance.www.hr.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.beans.MenuBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.service.IMenuService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 菜单
 * @version 2017/9/25
 * @author 崔棋
 *
 */
//页面调用方法层
@Controller
@RequestMapping("/menu")
public class MenuController {
	
	@Resource
	private IMenuService menuService;
	
	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getMenuByUrl.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object getMenuByUrl(HttpServletRequest request){
		String url=request.getParameter("url");
		if (StringUtils.isBlank(url)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(menuService.getMenuByUrl(url));
	}
	
	
	/**
	 * 查询菜单，返回树结构
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getMenuTree.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object getMenuTree(HttpServletRequest request){
		List<MenuBean> menuBeanList=menuService.getMenuTree();
		if(menuBeanList.size()>0){
			return ResultMap.success(menuBeanList);
		}else{
			return ResultMap.fault("30001","未查找到记录。");
		}
	}
	
	/**
	 * 用户登陆加载菜单，根据角色ID，查询权限菜单数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getMenuTreeByLoginUser.do", method = {RequestMethod.POST})
	@ResponseBody 
	public Object getMenuTreeByLoginUser(HttpServletRequest request){
		//获取登陆用户数据  
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		List<MenuBean> menuBeanList = new ArrayList<MenuBean>();
		if(userEmployee!=null){
			boolean isSalaryAuth = false;
			if (userEmployee.getPrivilegeGroupId() != 1) {
				isSalaryAuth = menuService.getSalaryAuth(userEmployee.getEmployeeId());
			}
			menuBeanList.addAll(menuService.getMenuTreeByLoginUser(userEmployee.getPrivilegeGroupId(), isSalaryAuth));	
		}
		else{
			menuBeanList.addAll(menuService.getMenuIndex());
		}
		return ResultMap.success(menuBeanList);
	}
	
	/**
	 * 查询菜单数据list
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getMenuList.do", method = {RequestMethod.POST})
	@ResponseBody 
	public Object getMenuList(HttpServletRequest request){
		List<MenuBean> menuList=menuService.getMenuList();
		return ResultMap.success(menuList);
	}

	/**
	 * 设置菜单树结构数据
	 * @param request
	 * @param menuBeanlist List菜单数据源
	 * @return
	 */
	@RequestMapping(value = "/addMenuTree.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object addMenuTree(HttpServletRequest request,@RequestBody List<MenuBean> menuBeanlist)
	{
		if(menuBeanlist.size()>0)
		{
			if(menuService.insertMenuTree(menuBeanlist)){
				return ResultMap.success();
			}
			else{
				return ResultMap.fault("30002", "操作错误!");
			}			
		}
		else{
			return ResultMap.fault("30002", "操作错误!");
		}
	}
	
}
